from flask import Blueprint
from flask import redirect
from flask import request
from flask import session
from flask import url_for

admin_blu = Blueprint("admin", __name__, url_prefix='/admin')

from . import views


@admin_blu.before_request
def before_request():
    """每次请求前判断用户身份"""

    # 请求不为管理员登陆请求
    if not request.url.endswith(url_for('admin.admin_login')):  # 判断请求 url结尾不为admin.admin_login,也就是登陆页面
        # 取值
        user_id = session.get('user_id', None)
        is_admin = session.get('is_admin', False)

        if not user_id or not is_admin:  # 管理员以外身份的跳转到项目主页
            return redirect('/')